package com.nbcio.modules.erp.coupon.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nbcio.modules.erp.coupon.service.IMallCouponService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.nbcio.modules.erp.coupon.entity.MallCoupon;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 优惠券
 * @Author: nbacheng
 * @Date:   2024-04-16
 * @Version: V1.0
 */
@Api(tags="优惠券")
@RestController
@RequestMapping("/coupon/mallCoupon")
@Slf4j
public class MallCouponController extends JeecgController<MallCoupon, IMallCouponService> {
	@Autowired
	private IMallCouponService mallCouponService;
	
	/**
	 * 分页列表查询
	 *
	 * @param mallCoupon
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "优惠券-分页列表查询")
	@ApiOperation(value="优惠券-分页列表查询", notes="优惠券-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(MallCoupon mallCoupon,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<MallCoupon> queryWrapper = QueryGenerator.initQueryWrapper(mallCoupon, req.getParameterMap());
		Page<MallCoupon> page = new Page<MallCoupon>(pageNo, pageSize);
		IPage<MallCoupon> pageList = mallCouponService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param mallCoupon
	 * @return
	 */
	@AutoLog(value = "优惠券-添加")
	@ApiOperation(value="优惠券-添加", notes="优惠券-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody MallCoupon mallCoupon) {
		if(mallCoupon.getStartDate() == null){
			mallCoupon.setStartDate(new Date());
		}
		mallCouponService.save(mallCoupon);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param mallCoupon
	 * @return
	 */
	@AutoLog(value = "优惠券-编辑")
	@ApiOperation(value="优惠券-编辑", notes="优惠券-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody MallCoupon mallCoupon) {
		mallCouponService.updateById(mallCoupon);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "优惠券-通过id删除")
	@ApiOperation(value="优惠券-通过id删除", notes="优惠券-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		mallCouponService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "优惠券-批量删除")
	@ApiOperation(value="优惠券-批量删除", notes="优惠券-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.mallCouponService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "优惠券-通过id查询")
	@ApiOperation(value="优惠券-通过id查询", notes="优惠券-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		MallCoupon mallCoupon = mallCouponService.getById(id);
		if(mallCoupon==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(mallCoupon);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param mallCoupon
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MallCoupon mallCoupon) {
        return super.exportXls(request, mallCoupon, MallCoupon.class, "优惠券");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MallCoupon.class);
    }


	 @AutoLog(value = "优惠券-查询所有符合条件优惠券")
	 @ApiOperation(value="优惠券-查询所有符合条件优惠券", notes="优惠券-查询所有符合条件优惠券")
	 @PostMapping(value = "/queryMallCoupon")
	 public Result<?> queryMallCoupon(@RequestBody Map<String,Object> map){

		 List<MallCoupon> mallCoupons = mallCouponService.queryMallCoupon(map);

		 return Result.OK(mallCoupons);
	 }


	 @AutoLog(value = "优惠券-查询用户领取的优惠券")
	 @ApiOperation(value="优惠券-查询用户领取的优惠券", notes="优惠券-查询用户领取的优惠券")
	 @PostMapping(value = "/queryUserCouponByUserId")
	 public Result<?> queryUserCouponByUserId(@RequestBody Map<String,Object> map){

		 List<MallCoupon> mallCoupons = mallCouponService.queryUserCouponByUserId(map);
		 return Result.OK(mallCoupons);
	 }


	 @AutoLog(value = "优惠券-领取优惠券")
	 @ApiOperation(value="优惠券-领取优惠券", notes="优惠券-领取优惠券")
	 @PostMapping(value = "/ReceiveCouponById")
	 public Result<?> ReceiveCouponById(@RequestBody Map<String,Object> map){

		 mallCouponService.ReceiveCouponById(map);
		 return Result.OK("领取优惠券成功");
	 }

	 @AutoLog(value = "订单-商城订单生成后处理")
	 @ApiOperation(value="订单-商城订单生成后处理", notes="订单-商城订单生成后处理")
	 @PostMapping(value = "/afterSaveOrder")
	 public Result<?> afterSaveOrder(@RequestBody Map<String,Object> map){

		 mallCouponService.afterSaveOrder(map);
		 return Result.OK("订单生成成功");
	 }



 }
